#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

/*
int main()
{
    FILE *fp = fopen("myfile","w+");
    if(!fp)
    {
        printf("fopen error!\n");
    }

    const char *msg = "hello world!\n";
    int count = 5;
    while(count--)
    {
        fwrite(msg,1,strlen(msg),fp);
    }

    fclose(fp);
}
*/

/*
int main(int argc,char *argv[])
{
    if(argc != 2)
    {
        printf("argv error!\n");
        return 1;
    }

    for(int i = 0;i<argc;i++)
    {
        printf("argv[%d]:%s\n",i,argv[i]);
    }

    FILE *fp = fopen(argv[1],"w+");

    const char *msg = "hello world!\n";
    int count = 5;
    while(count--)
    {
        fwrite(msg,1,strlen(msg),fp);
    }

    fseek(fp,0L,SEEK_SET);
    char buffer[1024];
    size_t n = fread(buffer,1,sizeof(buffer),fp);
    buffer[n] = 0;
    if(feof(fp))
    {
        printf("文件成功读取\n");
    }
    fclose(fp);
    printf("%s",buffer);
}
*/

/*
int main()
{
    const char *msg1 = "hello fwrite\n";
    fwrite(msg1,1,strlen(msg1),stdout);

    fprintf(stdout,"hello fprintf\n");

    const char *msg2 = "hello fputs\n";
    fputs(msg2,stdout);

    const char *msg3 = "hello puts\n";
    puts(msg3);
}
*/

/*
#define ONE 0001 //0000 0001
#define TWO 0002 //0000 0010
#define THREE 0004 //0000 0100

void func(int flags)
{
    if(flags & ONE) printf("flags has ONE! ");
    if(flags & TWO) printf("flags has TWO! ");
    if(flags & THREE) printf("flags has THREE! ");
    printf("\n");
}

int main()
{
    func(ONE);
    func(THREE);
    func(ONE|TWO|THREE);
    func(ONE|THREE);
}
*/

/*
int main()
{
    int fd = open("myfile", O_RDONLY);
    if(fd < 0){
    perror("open");
    return 1;
    }
    const char *msg = "hello bit!\n";
    char buf[1024];
    int x = 5;
    ssize_t s = 0;
    s = read(fd, buf, sizeof(buf));//类⽐write
    buf[s] = 0;
    printf("%s",buf);
    close(fd);

    return 0;
}*/

/*
int main()
{
    char buf[1024];
    ssize_t s = read(0,buf,sizeof(buf));

    buf[s] = 0;
    write(1,buf,strlen(buf));
    write(2,buf,strlen(buf));
}
*/

/*
int main()
{
    int fd = open("myfile",O_RDONLY);

    printf("%d\n",fd);
}
*/

/*
int main()
{
    close(0);
    int fd = open("myfile",O_RDONLY);

    close(fd);
    printf("%d\n",fd);
}
*/

/*
int main()
{
    close(1);
    int fd = open("myfile",O_WRONLY|O_CREAT,0064);

    if(fd<0)
    {
        perror("open");
        return 1;
    }
    printf("fd: %d\n",fd);
    fflush(stdout);
    close(fd);
    _exit(0);
}
*/

/*
int main() {
    close(1);
    int fd = open("log.txt", O_WRONLY | O_CREAT , 0666);
    if (fd < 0)
    {
        perror("open");
        return 0;
    }

    const char*msg = "hello world\n";
    //printf("hello world: %d\n", fd);

    printf("%d\n",fd);
    write(fd,msg,strlen(msg));
    fflush(stdout);
    close(fd);
    return 0;
}
*/

/*
int main()
{
    close(2);
    int fd = open("log.txt", O_WRONLY | O_CREAT | O_TRUNC, 0666);
    if (fd < 0)
    {
        perror("open");
        return 0;
    }

    perror("hello world");
    close(fd);
    return 0;
}
*/

/*
#include <stdio.h>
#include <unistd.h>

int main() {
    int x = 10;  // 在 fork 之前的代码
    printf("Before fork: x = %d\n", x);
    int pid = fork();
    if (pid < 0) {
        perror("fork failed");
        return 1;
    } else if (pid == 0) {
        // 子进程代码
        x = 20;  // 子进程修改 x 的值
        printf("Child process: x = %d\n", x);
    } else {
        // 父进程代码
        x = 30;  // 父进程修改 x 的值
        printf("Parent process: x = %d\n", x);
    }
    printf("After fork: x = %d\n", x);
    return 0;
}
*/

/*
#include <stdio.h>
#include <string.h>
int main()
{
    const char *msg0="hello printf\n";
    const char *msg1="hello fwrite\n";
    const char *msg2="hello write\n";
    printf("%s", msg0);
    fwrite(msg1, strlen(msg0), 1, stdout);
    write(1, msg2, strlen(msg2));
    fork();
    return 0;
}
*/

#include <stdio.h>
#include <unistd.h>

int main()
{
    if (chdir("/mnt/hgfs/CCCC的学习") == -1)
    {
        perror("chdir");
        return 1;
    }
    // 可以添加一些代码来验证是否切换成功，比如打印当前目录
    char cwd[1024];
    if (getcwd(cwd, sizeof(cwd)) == NULL)
    {
        perror("getcwd");
        return 1;
    }
    printf("Current working directory: %s\n", cwd);
    return 0;
}
